'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('system_configs', {
      id: {
        type: Sequelize.UUID,
        defaultValue: Sequelize.UUIDV4,
        primaryKey: true,
      },
      tenant_id: {
        type: Sequelize.UUID,
        allowNull: true,
        references: {
          model: 'tenants',
          key: 'id',
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE',
        comment: '租户ID，null表示全局配置',
      },
      config_key: {
        type: Sequelize.STRING(100),
        allowNull: false,
        comment: '配置键',
      },
      config_value: {
        type: Sequelize.TEXT,
        allowNull: true,
        comment: '配置值（JSON格式）',
      },
      config_type: {
        type: Sequelize.STRING(50),
        allowNull: false,
        defaultValue: 'string',
        comment: '配置类型：string, number, boolean, json',
      },
      description: {
        type: Sequelize.STRING(500),
        allowNull: true,
        comment: '配置说明',
      },
      is_system: {
        type: Sequelize.BOOLEAN,
        defaultValue: false,
        comment: '是否为系统配置',
      },
      created_at: {
        type: Sequelize.DATE,
        allowNull: false,
      },
      updated_at: {
        type: Sequelize.DATE,
        allowNull: false,
      },
    });

    // 添加唯一索引
    await queryInterface.addIndex('system_configs', ['tenant_id', 'config_key'], {
      unique: true,
      name: 'uk_tenant_config_key',
    });

    // 添加配置键索引
    await queryInterface.addIndex('system_configs', ['config_key'], {
      name: 'idx_config_key',
    });
  },

  down: async (queryInterface, Sequelize) => {
    await queryInterface.dropTable('system_configs');
  },
};

